﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Runtime.InteropServices;
using System.Data.SqlClient;


namespace Turtle8
{
    public partial class MainMenuGUI : Form
    {
        CompanyManager companyManager = CompanyManager.getInstance();
        SoldierCard soldierCard = new SoldierCard();
        CompanyCard companyCard = new CompanyCard();
        TahasukaCard tahasukaCard = new TahasukaCard();
        PracticeCard practiceCard = new PracticeCard();
        LibaryPanel libaryPanel = new LibaryPanel();
        QueryCard queryCard;

        public MainMenuGUI()
        {
            queryCard = new QueryCard();
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;

            Application.CurrentInputLanguage = InputLanguage.FromCulture(new System.Globalization.CultureInfo("he"));            
        }

        private void changeMainPanel(Control ctrl)
        {
            if (!splitContainer2.Panel2.Controls.Contains(ctrl))
            {
                this.splitContainer2.Panel2.Controls.Clear();
                this.splitContainer2.Panel2.Controls.Add(ctrl);
            }
        }

        private void buttonCompany_Click(object sender, EventArgs e)
        {
            changeMainPanel(companyCard);
            companyCard.refresh();
        }

        private void buttonPractice_Click(object sender, EventArgs e) { changeMainPanel(practiceCard); }
        private void buttonTahasuka_Click(object sender, EventArgs e) { changeMainPanel(tahasukaCard); }
        private void buttonLibrary_Click(object sender, EventArgs e) { changeMainPanel(libaryPanel); }
        private void buttonQuery_Click(object sender, EventArgs e) { changeMainPanel(queryCard); }
        private void buttonSoldier_Click(object sender, EventArgs e) { changeMainPanel(soldierCard); }

        private void ToolStripMenuItemHelpAbout_Click(object sender, EventArgs e)
        { MessageBox.Show(".פלוגתון - גירסא 12.09. שי נווה"); }

        private void buttonMain_Click(object sender, EventArgs e)
        { changeMainPanel(pictureBox); }

        private void ToolStripMenuItemBackup_Click(object sender, EventArgs e)
        {
            string backupStr = CompanyManager.getSqlStringFromFile(@"\other\backup.sql");
            string dateTimeStr = DateTime.Today.ToShortDateString();
            dateTimeStr = dateTimeStr.Replace(@"/", "-");
            backupStr = backupStr.Replace("@path", CompanyManager.getValueFromInitFile("BackupPath") + @"\cm" + dateTimeStr + ".bak");

            SqlCommand backupCmd = SqlManager.SqlCommandFactory(backupStr);
            try
            {
                backupCmd.ExecuteNonQuery();
                MessageBox.Show(".קובץ גיבוי נוצר בהצלחה");
            }
            catch (Exception)
            {
                MessageBox.Show(".אירעה שגיאה. לא נוצר קובץ גיבוי");
            }
        }

        private void ToolStripMenuItemRestore_Click(object sender, EventArgs e)
        {
            string restoreStr = CompanyManager.getSqlStringFromFile(@"\other\restore.sql");
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = CompanyManager.getValueFromInitFile("BackupPath");

            if ((dlg.ShowDialog() == DialogResult.OK) && dlg.FileName.StartsWith(CompanyManager.getValueFromInitFile("BackupPath")))
            {
                if (MessageBox.Show("?האם אתה בטוח כי ברצונך לשחזר לגירסא זו", "שחזור נתונים", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    progressBar.Visible = true;
                    progressBar.Refresh();
                  
                    restoreStr = restoreStr.Replace("@path", dlg.FileName);

                    try
                    {
                        SqlCommand restoreCmd = SqlManager.SqlCommandFactory(restoreStr);
                        restoreCmd.ExecuteNonQuery();
                        progressBar.Value = progressBar.Maximum;
                        MessageBox.Show(".שחזור בוצע בהצלחה");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(".אירעה שגיאה. לא בוצע שחזור" + ex.Message);
                    }
                    finally
                    {
                        progressBar.Value = progressBar.Minimum;
                        progressBar.Visible = false;
                        SqlManager.Connection.Close();
                        SqlManager.Connection.Open();
                    }
                }
            }
        }
    }
}

    


